#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging

from dragon.selector import Selector
from dragon.page import DraPage
import requests

class LoginSe:
    phoneInput= Selector('input[placeholder="请输入手机号"]','手机号输入框')
    # self.sendCodeButton= Selector('div.cp','获取验证码按钮')
    sendCodeButton= Selector('text=获取验证码','获取验证码按钮')
    codeInput= Selector('input[placeholder="请输入验证码"]','验证码输入框')
    sendCodeAlert=Selector('.el-message__content','发送验证码提示')
    submitButton= Selector('#app > div > div.el-button.el-button--large > span','登录按钮')
    submitAlert= Selector('.el-message__content','登录提示')
    giftPrompt=Selector('div.tishiyu','免费天数提示语')
    
class LoginPage:
    def __init__(self, draPage: DraPage):
        self.draPage=draPage
        self.phoneInput = draPage.locator(LoginSe.phoneInput)
        self.sendCodeButton = draPage.locator(LoginSe.sendCodeButton)
        self.codeInput = draPage.locator(LoginSe.codeInput)
        self.sendCodeAlert = draPage.locator(LoginSe.sendCodeAlert)
        self.submitButton = draPage.locator(LoginSe.submitButton)
        self.submitAlert = draPage.locator(LoginSe.submitAlert)
        self.giftPrompt = draPage.locator(LoginSe.giftPrompt)

    def _pre_login(self,phone:str,base_url):
        '''用于自动化测试前置步骤'''
        url=f'{base_url}/api/data/site/f59a607c'
        '''获取验证码接口地址'''
        data={
            "phone": phone,
            "platform": "h5"
        }
        res=requests.post(url,json=data).json()
        code=res['result']['data']['code']
        self.phoneInput.fill(phone)
        self.codeInput.fill(code)
        self.submitButton.click()
        self.submitButton.should().disappear()
        logging.info('登录成功')
        return True


